package net.bolue.sysmng.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import net.bolue.sysmng.exception.BolueException;

/**
 * 映射工具类
 * @author Administrator
 *
 */
public class ReflectHelper {

	/**
	 * 实例化类
	 * @param className
	 * @return
	 * @throws BolueException
	 */
	 public static Object classNewInstance(String className) throws BolueException{
		 Class localClass = classForName(className);
		 Object localObject = null;
		 try{
			 localObject =  localClass.newInstance();
		 }catch (Exception localException){
			 throw new BolueException(1, "实例化类" + className + "出错！",localException);
		 }
		 return localObject;
	 }
	 
	 /**
	  * 通过类名获得类
	  * @param className
	  * @return
	  * @throws BolueException
	  */
	 public static Class classForName(String className) throws BolueException{
		 Class localClass = null;
		 try{
			 ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader();
			 if (localClassLoader == null){
				 localClassLoader = ReflectHelper.class.getClassLoader();
			 }
			 localClass = Class.forName(className, true, localClassLoader);
		 }catch (ClassNotFoundException localClassNotFoundException){
			 throw new BolueException(1, "没有找到类名为" + className + "的类!");
		 }
		 return localClass;
	 }
	 
	 public static String getFullClassName(String paramString){
		 if (paramString.indexOf(".") >= 0){
			 return paramString;
		 }
		 int i = 0;
		 if ((paramString.startsWith("[L")) && (paramString.endsWith(";"))){
			 i = 1;
			 paramString = paramString.substring(2, paramString.length() - 1).trim();
		 }
		 String str = null;
		 if (paramString.equals("String")){
			 str = "java.lang.String";
		 }else if (paramString.equals("List")){
			 str = "java.util.List";
		 }else if (paramString.equals("Date")){
			 str = "java.util.Date";
		 }else if (paramString.equals("Timestamp")){
			 str = "java.sql.Timestamp";
		 }else if (paramString.equals("Intgeger")){
			 str = "java.lang.Integer";
		 }else if (paramString.equals("Long")){
			 str = "java.lang.Long";
		 }else if (paramString.equals("Float")){
			 str = "java.lang.Float";
		 }else if (paramString.equals("Double")){
			 str = "java.lang.Double";
		 }else if (paramString.equals("BigDecimal")){
			 str = "java.math.BigDecimal";
		 }else if (paramString.equals("BigDecimal")){
			 str = "java.math.BigDecimal";
		 }else if (paramString.equals("Object")) {
			 str = "java.lang.Object";
		 }
		 if (str == null){
			 str = paramString;
		 }
		 if (i != 0){
			 str = "[L" + str + ";";
		 }
		 return str;
	 }
	 
	 public static Object invokeMethod(Object objectInstance, String methedStr, Object[] paramArray) throws BolueException{
		 String methedName = null;
		 String paramrStr = null;
		 int i = methedStr.indexOf("(");
		 if (i == -1) {
			 methedName = methedStr;
		 }else {
			 methedName = methedStr.substring(0, i).trim();
			 paramrStr = methedStr.substring(i + 1);
			 i = paramrStr.indexOf(")");
			 if (i >= 0){
				 paramrStr = paramrStr.substring(0, i);
			 }
		 }
		 String[] paramTypeArray = null;
		 if ((paramrStr == null) || (paramrStr.trim().length() == 0)) {
			 paramTypeArray = new String[0];
		 }else {
			 String[] s = paramrStr.split(",");
			 paramTypeArray = new String[s.length];
			 for (int j = 0;j<s.length; j++){
				 paramTypeArray[j] = getFullClassName(s[j]);
			 }
		 }
		 return invokeMethod(objectInstance, methedName, paramTypeArray, paramArray);
	 }
	 
	 public static Object invokeMethod(Object objectInstance, String methedName, String[] paramTypeArray, 
			 Object[] paramArray) throws BolueException{
		 if (paramTypeArray == null){
			 paramTypeArray = new String[0];
		 }
		 if (paramArray == null){
			 paramArray = new String[0];
		 }
		 if (paramTypeArray.length != paramArray.length) {
			 throw new BolueException(5,"参数paramTypeNames与paramVals元素个数必须相同!");
		 }
		 Class localClass = objectInstance.getClass();
		 Class[] arrayOfClass = new Class[paramTypeArray.length];
		 for (int i=0;i<paramTypeArray.length;i++){
			 arrayOfClass[i] = classForName(paramTypeArray[i]);
		 }
		 Method localMethod = null;
		 try {
			localMethod = localClass.getMethod(methedName, arrayOfClass);
		 } catch (Exception e) {
			String msg = "";
			for (int i=0;i<arrayOfClass.length;i++){
				msg = msg + (i == 0 ? "" : ",") + arrayOfClass[i].getName();
			}
			throw new BolueException(1, "没有方法" + localClass.getName() + "." + methedName + "(" + msg + ")，请纠正!",e);
		 }
		 try {
			return localMethod.invoke(objectInstance, paramArray);
		 } catch (Exception e) {
			 throw new BolueException(1, "执行方法" + localClass.getName() + "." + methedName + "出错!",e);
		 }
		
	 }
	 
	 /**
	  * 首字母转大写
	  * @param fildeName
	  * @return
	  * @throws BolueException
	  */
	 public static String toUpperCaseFirstOne(String fildeName) throws BolueException{
		 byte[] items = fildeName.getBytes();
		 items[0] = (byte) ((char) items[0] - 'a' + 'A');
		 return new String(items);
	 }
	 
	 /**
	  * 获得get方法名
	  * @param fildeName
	  * @return
	  * @throws BolueException
	  */
	 public static String getMethodName(String fildeName) throws BolueException{
		 String s = toUpperCaseFirstOne(fildeName);
		 return "get"+s;
	 }
	 
	 /**
	  * 获得set方法名
	  * @param fildeName
	  * @return
	  * @throws BolueException
	  */
	 public static String setMethodName(String fildeName) throws BolueException{
		 String s = toUpperCaseFirstOne(fildeName);
		 return "set"+s;
	 }
	 
}
